package com.woniuxy.house.controller;


import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.house.HouseType;
import com.woniuxy.entity.house.HouseType;
import com.woniuxy.entity.house.dto.HouseInfoDto;
import com.woniuxy.house.service.HouseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Api(tags = "户型接口",description = "管理户型的接口")
@RestController
@RequestMapping("/house/houseType")
public class HouseTypeController {

    @Autowired
    private HouseTypeService houseTypeService;

    /**
     * 根据id查询户型图
     * @param id 户型id
     * @return
     */
    @ApiOperation("根据id查询户型图")
    @GetMapping("/find")
    public ResponseResult<HouseType> findHouseTypeById(int id){
        HouseType houseType = houseTypeService.findTypeById(id);
        if(houseType!=null){
           return new ResponseResult<>(houseType);
        }else{
            return new ResponseResult(500,"fail");
        }
    }

    /**
     * 根据id删除户型图
     * @param id 户型id
     * @return
     */
    @ApiOperation("根据id删除户型图")
    @GetMapping("/delete")
    public ResponseResult deleteTypeById(int id){
        ResponseResult responseResult = new ResponseResult();
        if (houseTypeService.deleteTypeById(id)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 新增户型图
     * @param houseType
     * @return
     */
    @ApiOperation("新增户型图")
    @PostMapping("/add")
    public ResponseResult addType(HouseType houseType){
        System.out.println(houseType);
        if(houseTypeService.insertType(houseType)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 修改户型
     * @param houseType
     * @return
     */
    @ApiOperation("修改户型")
    @PostMapping("/update")
    public ResponseResult updateType(HouseType houseType){
        if(houseTypeService.updateType(houseType)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }

    }


    /**
     * 根据卧室数量查询房屋展示信息
     * @param bedRoom  卧室数量：例如 两室/三室
     * @return
     */
    @ApiOperation("根据卧室数量查询房屋展示信息")
    @GetMapping("//findBuildingByType")
    public ResponseResult<List<HouseInfoDto>> findBuildingByType(String bedRoom,int current,int size){
        System.out.println(bedRoom);
        List<HouseInfoDto> houseInfoDtos = houseTypeService.findHouseByBedroom(bedRoom,current,size);
        if(houseInfoDtos.size()>0){
            return new ResponseResult<>(houseInfoDtos);
        }else{
            return new ResponseResult<>(500,"fail");
        }
    }


}

